package com.xtyhz.controller;


import com.xtyhz.model.R;
import com.xtyhz.pojo.User;
import com.xtyhz.pojo.dto.LoginDto;
import com.xtyhz.server.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name = "登录管理", description = "登录相关的业务")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/getCode")
    @Operation(summary = "验证码", description = "验证码登录加载")
    public R<Map<String,String>> getCode(){
        return userService.getCode();
    }

    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录方法")
    public R<Map<String,String>> login(@RequestBody LoginDto loginDto){
        return userService.login(loginDto);
    }

    @PostMapping("/refreshToken")
    @Operation(summary = "刷新token", description = "刷新token的方法")
    @Parameter(name = "refreshToken", description = "刷新token", required = true)
    public R<Map<String,String>> refreshToken(@RequestBody Map<String,String> refreshToken){
        return userService.refreshToken(refreshToken);
    }

    @GetMapping("/list")
    @Operation(summary = "用户查询", description = "查询所有管理端的用户")
    @Parameters({
            @Parameter(name = "page", description = "当前页", required = true),
            @Parameter(name = "size", description = "每页大小", required = true),
            @Parameter(name = "nickname", description = "用户姓名", required = false),
            @Parameter(name = "email", description = "邮箱", required = false),
            @Parameter(name = "phone", description = "电话", required = false),
    })
    public R<List<User>> list(@RequestParam Integer page,@RequestParam Integer size,String nickname,String email,String phone){
        return userService.list(page,size,nickname,email,phone);
    }

    @PostMapping("/add")
    @Operation(summary = "用户添加", description = "添加管理端的用户")
    @Parameters({
            @Parameter(name = "username", description = "用户账号", required = true),
            @Parameter(name = "username", description = "用户账号", required = true),
            @Parameter(name = "nickname", description = "用户姓名", required = true),
            @Parameter(name = "password", description = "用户密码", required = true),
            @Parameter(name = "phone", description = "电话", required = false),
            @Parameter(name = "email", description = "邮箱", required = false),
    })
    public R<String> add(@RequestBody User user){
        return userService.add(user);
    }

    @PutMapping("/update")
    @Operation(summary = "用户修改", description = "修改管理端的用户")
    @Parameters({
            @Parameter(name = "userId", description = "用户Id", required = true),
            @Parameter(name = "username", description = "用户账号", required = true),
            @Parameter(name = "nickname", description = "用户姓名", required = true),
            @Parameter(name = "phone", description = "电话", required = false),
            @Parameter(name = "email", description = "邮箱", required = false),
    })
    public R<String> update(@RequestBody User user){
        return userService.update(user);
    }

    @DeleteMapping("/delete/{userId}")
    @Operation(summary = "用户删除", description = "删除管理端的用户")
    @Parameter(name = "userId", description = "用户Id", required = true)
    public R<String> delete(@PathVariable(value = "userId") Integer UserId){
        return userService.delete(UserId);
    }


    @GetMapping("/test")
    public String test(){
        return "成功";
    }
}
